package statistics;

import main.Simulator;
import event.ACKReceivedEvent;
import event.ACKTimeoutEvent;
import event.Event;

public class CwndMssStatistics extends Statistics {

	public CwndMssStatistics(Simulator simulator, String name, String xLabel, String yLabel, int n) {
		super(simulator, name, xLabel, yLabel, n);
		this.title = name + "\n" + "cwnd/mss";
		this.simulator = simulator;
		
		// Inicializa o gráfico
		for (int i = 0; i < n; i++) {
			xySeries[i].add(0, 1);
		}
	}

	@Override
	public void collect(Event event) {
		if (!(event instanceof ACKReceivedEvent) && !(event instanceof ACKTimeoutEvent))
			return;
		
		int sessionId = 0;
		if (event instanceof ACKReceivedEvent) {
			sessionId = ((ACKReceivedEvent) event).getACK().getPack().getSessionId();
		} else if (event instanceof ACKTimeoutEvent) {
			sessionId = ((ACKTimeoutEvent) event).getPack().getSessionId();
		}
		
		double cwnd = simulator.getTransmitters().get(sessionId).getCwnd();
		int MSS = simulator.getProperties().getMSS();
		xySeries[sessionId].add(simulator.getTime(), cwnd/MSS);
	}
	
	public void saveAsFile() {
		saveAsImage(name + "-" + "CwndMss");
	}

}
